<template>
  <div class="pdf-viewer-container" v-if="showPreview">
    <div class="pdf-viewer-header">
      <h3>📄 PDF 简历预览</h3>
      <button @click="closePreview" class="close-btn">
        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <line x1="18" y1="6" x2="6" y2="18"></line>
          <line x1="6" y1="6" x2="18" y2="18"></line>
        </svg>
      </button>
    </div>
    <div class="pdf-viewer-content">
      <iframe 
        :src="iframeSrc" 
        frameborder="0" 
        class="pdf-iframe"
        @load="handleLoad"
        @error="handleError"
      ></iframe>
      <div v-if="error" class="pdf-error">
        <p>{{ error }}</p>
        <a :href="pdfUrl" target="_blank" class="pdf-fallback-link">在新窗口打开 PDF</a>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'

const props = defineProps({
  pdfUrl: {
    type: String,
    required: true
  },
  show: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['close'])

const showPreview = ref(props.show)
const error = ref('')

// 计算 iframe src，使用 #toolbar=0 隐藏浏览器默认工具栏，获得更简洁的预览
const iframeSrc = computed(() => {
  if (!props.pdfUrl) return ''
  // 对于同域的 PDF，直接使用；对于跨域或其他情况，使用浏览器原生查看器
  return props.pdfUrl + '#toolbar=0&navpanes=0&scrollbar=1'
})

watch(() => props.show, (newVal) => {
  showPreview.value = newVal
  if (!newVal) {
    error.value = ''
  }
})

const handleLoad = () => {
  error.value = ''
}

const handleError = () => {
  error.value = 'PDF 加载失败，请尝试下载后查看'
}

const closePreview = () => {
  showPreview.value = false
  emit('close')
}
</script>

<style scoped>
.pdf-viewer-container {
  margin: 2rem 0;
  border: 2px solid var(--vp-c-divider);
  border-radius: 12px;
  overflow: hidden;
  background: var(--vp-c-bg-soft);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.pdf-viewer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  background: var(--vp-c-bg);
  border-bottom: 1px solid var(--vp-c-divider);
}

.pdf-viewer-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--vp-c-text-1);
}

.close-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: var(--vp-c-text-2);
}

.close-btn:hover {
  background: var(--vp-c-bg-soft);
  color: var(--vp-c-text-1);
}

.pdf-viewer-content {
  padding: 0;
  background: white;
  display: flex;
  flex-direction: column;
  position: relative;
  min-height: 800px;
}

.pdf-iframe {
  width: 100%;
  height: 100vh;
  min-height: 900px;
  border: none;
  background: white;
}

.pdf-error {
  padding: 2rem;
  text-align: center;
  color: var(--vp-c-danger);
}

.pdf-fallback-link {
  display: inline-block;
  margin-top: 1rem;
  padding: 0.5rem 1rem;
  background: var(--vp-c-brand-1);
  color: white;
  text-decoration: none;
  border-radius: 6px;
  transition: all 0.2s ease;
}

.pdf-fallback-link:hover {
  background: var(--vp-c-brand-2);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .pdf-iframe {
    height: 80vh;
    min-height: 600px;
  }
}

.dark .pdf-viewer-content {
  background: var(--vp-c-bg);
}
</style>

